<?php

require_once eCMS_DIR . '/app/lib/smarty/Smarty.class.php';
require_once eCMS_DIR . '/app/lib/PHPMailer/class.phpmailer.php';

class SmartyPHPMailer extends PHPMailer  {
	
	private $smarty, $useSmarty=0, $tplFile; 		//My own Smarty
	
	function __construct() {
		
		//Set up my phpMailer
		
		//TODO: User the Page-COntext to load Settings (ModuleInstance specific)
		
		//Required
		$this->From     = Runtime::getSetting("mailer.from");
		$this->FromName = Runtime::getSetting("mailer.fromName");
		$this->Sender   = Runtime::getSetting("mailer.sender");
		$this->Mailer   = "smtp";
		$this->Host     = Runtime::getSetting("mailer.host");
		
		//Authentication: Optional
		$this->Username = Runtime::getSetting("mailer.username");
		$this->Password = Runtime::getSetting("mailer.password");
		if($this->Username && $this->Password) { $this->SMTPAuth = true; }
		
		//Optional
		$this->Subject  = Runtime::getSetting("mailer.defsubject");
		$this->DebugTo  = Runtime::getSetting("mailer.debugTo");
		$this->CopyTo  = Runtime::getSetting("mailer.copyTo");
		
		//For Logging
		$this->Recipients = array();
	}
	
	
	function setSmartyTemplate($tpl) {
		
		if(file_exists($tpl)) {
			
			$this->tplFile =  $tpl;
			$cfg = Runtime::getConfiguration();
			
			$this->smarty = new Smarty();
			$this->smarty->compile_dir = $cfg["paths"]["tmp"] . "/cache/smarty";
			$this->smarty->plugins_dir = eCMS_DIR . "/app/lib/smarty/plugins";

			//Asign Default Variables
			$this->smarty->assign("SYS_DomainURL", Runtime::getSetting("DomainUrl"));
			
			$this->useSmarty = true;
			
		}
	}
	
	function assign($tpl_var, $value) {
		$this->smarty->assign($tpl_var, $value);
	}
	
	public function MsgHTML($message,$basedir='') {
		
		//Render message with the Smarty if is set, so this here becouse the orgin MsgHTML method embeds
		if($this->useSmarty) {
			
			$this->smarty->assign("SYS_Message", $message);
			$this->smarty->assign("SYS_Subject", $this->Subject);
			
			$message = $this->smarty->fetch($this->tplFile);
			
		}
		
		if($basedir=='') {
			$basedir = dirname($this->tplFile);
		}
		
		parent::MsgHTML($message, $basedir);
	}
	
	private function AddRecipient($mode, $address, $name) {
		$index = count($this->Recipients);

		$this->Recipients[$index]["mode"] = $mode;
		$this->Recipients[$index]["address"] = $address;
		$this->Recipients[$index]["name"] = $name;
		
	}
	
	public function AddAddress($address, $name) {
		$this->AddRecipient("To", $address, $name);

		if($this->DebugTo) {
			$name = $name." (".$address.")";
			$address = $this->DebugTo;
		}
		parent::AddAddress($address, $name);
	} 
	
	public function AddCC($address, $name) {
		$this->AddRecipient("CC", $address, $name);
		
		if($this->DebugTo) {
			$name = $name." (".$address.")";
			$address = $this->DebugTo;
					}
		parent::AddCC($address, $name);
	} 

	public function AddBCC($address, $name) {
		$this->AddRecipient("BCC", $address, $name);
		
		if($this->DebugTo) {
			$name = $name." (".$address.")";
			$address = $this->DebugTo;
		}
		parent::AddBCC($address, $name);
	} 
	
	public function Send() {
		
		if($this->CopyTo && $this->DebugTo=="")
		{
			$this->AddBCC($this->CopyTo, "Copy to: " . $this->copyTo);	
		}
		
		$vardata = array();
		$vardata["Recipients"] = $this->Recipients;
		$vardata["From"] = $this->From;
		$vardata["FromName"] = $this->FromName;
		$vardata["SenderMail"] = $this->Sendmail;
		$vardata["DebugTo"] = $this->DebugTo;
		$vardata["CopyTo"] = $this->CopyTo;
		$vardata["Body"] = $this->Body;
		
		if($this->DebugTo) {
			$this->Subject.=" (Debug)";
		}
		
		if(parent::Send()) {
			logger::logEvent("MailSendSuccess", __FILE__, __CLASS__, __FUNCTION__, __LINE__, $vardata, null);
			return true;
		}
		else {
			logger::logEvent("MailSendFailed", __FILE__, __CLASS__, __FUNCTION__, __LINE__, $vardata, null);
			return false;
		}
	}
}

?>